//
//  HotelTagViewModel.swift
//  AirTravel
//
//  Created by apple on 2021/7/19.
//

import UIKit

protocol HotelTagDelegate:NSObjectProtocol {
    func HotelTagClick(data:NSDictionary)
}
class HotelTagViewModel: NSObject {
    
    var ticketdataSouce : [CityModel]?
    var dataSouce : Array<String>?
    weak var delegate: HotelTagDelegate?
    var collView : UICollectionView?
    let identifyTc = "TagColl"
    var number = 0
    
    
    func bindViewToViewModel(collView : UICollectionView, number:Int) {
        self.collView = collView
        self.collView?.dataSource = self
        self.collView?.delegate = self
        self.number = number
        if number == 0 || number == 2{//小标签
            let flowLayout = JYEqualCellSpaceFlowLayout(AlignType.left, 7.0)
            self.collView?.collectionViewLayout = flowLayout
        }else if number == 3{
            let layout = UICollectionViewFlowLayout.init()
            layout.scrollDirection = .vertical
            
            layout.itemSize = .init(width: (SCREEN_WIDTH - 50) / 3, height: 40)
            layout.minimumInteritemSpacing = 8
            layout.minimumLineSpacing = 10
            layout.sectionInset = UIEdgeInsets.init(top: 10, left: 15, bottom: 10, right: 15)
            self.collView?.isScrollEnabled = false
            self.collView?.collectionViewLayout = layout
        }
        else{
            let flowLayout = JYEqualCellSpaceFlowLayout(AlignType.left, 10.0)
            self.collView?.collectionViewLayout = flowLayout
        }
        self.collView?.isScrollEnabled = false
        
        self.collView?.showsVerticalScrollIndicator = true
        
        self.collView?.register(UINib.init(nibName: "TagColl", bundle: nil), forCellWithReuseIdentifier: identifyTc)
    }
    
    func setUpTicketdata(arrat:[CityModel]) {
        self.ticketdataSouce = arrat
        self.collView?.reloadData()
    }
    
    func setUpdata(arrat:Array<String>) {
        self.dataSouce = arrat
        self.collView?.reloadData()
    }
}

extension HotelTagViewModel:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if(number == 3){
            return self.ticketdataSouce?.count ?? 0
        }else{
            return self.dataSouce?.count ?? 0
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifyTc, for: indexPath) as! TagColl
   
            if self.number == 0 {
                cell.contentLab.font = UIFont.systemFont(ofSize: 10)
                cell.contentLab.text = dataSouce?[indexPath.row]
            }else if self.number == 2{
                cell.contentLab.font = UIFont.systemFont(ofSize: 10)
                cell.contentLab.layer.cornerRadius = 2
                cell.contentLab.layer.borderColor = UIColor(red: 165.0 / 255.0, green: 165.0 / 255.0, blue: 165.0 / 255.0, alpha: 1.0).cgColor
                cell.contentLab.layer.borderWidth = 1
                cell.contentLab.backgroundColor = UIColor.white
                cell.contentLab.text = dataSouce?[indexPath.row]
            }else if self.number == 3{
                cell.contentLab.font = UIFont.systemFont(ofSize: 16)
                cell.contentLab.backgroundColor = UIColor.white
                cell.contentLab.layer.cornerRadius = 3
                cell.contentLab.layer.borderColor = UIColor(red: 229.0 / 255.0, green: 229.0 / 255.0, blue: 229.0 / 255.0, alpha: 1.0).cgColor
                cell.contentLab.layer.borderWidth = 1;
                print("\(ticketdataSouce?[indexPath.row].city_name)")
                
                
                cell.contentLab.text = ticketdataSouce?[indexPath.row].city_name
                
                if(indexPath.row == 0){
                    cell.contentLab.text = "全部"
                }
            }
            else{
                cell.contentLab.font = UIFont.systemFont(ofSize: 12)
                cell.contentLab.text = dataSouce?[indexPath.row]
            }
            
        
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        
            
            if self.number == 0 || self.number == 2{
                if let array = self.dataSouce {
                    
                    let attr_value = "\(array[indexPath.row])"
                    var width = getLabWidth(labelStr: attr_value, font: UIFont.systemFont(ofSize: 10), height: 15)
                    if width < 20 {
                        width = 20
                    }
                    return CGSize.init(width: width + 10, height: 15)
                }
                
                
                
               
            }else if self.number == 3{
                return CGSize.init(width: (SCREEN_WIDTH - 50) / 3, height: 40)
            }
            else{
                
                if let array = self.dataSouce {
                    let attr_value = "\(array[indexPath.row])"
                    var width = getLabWidth(labelStr: attr_value, font: UIFont.systemFont(ofSize: 12), height: 30)
                    if width < 40 {
                        width = 40
                    }
                    return CGSize.init(width: width + 36, height: 30)
                }
            }
            
        
        return CGSize.init(width: 0, height: 0)
    }
    
}
